<template>
 <lay-layout class="Pop">
    
    <lay-body>
      <lay-table 
            id="Id"
            :columns="columns"
            :dataSource="dataSource"
            :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys"
            even
            @row="rowClick"
         
           
          >
        </lay-table>

    </lay-body>
    <lay-footer>
      <lay-switch v-model="IsJava" onswitch-text="Java"  unswitch-text="Java"></lay-switch>

      <lay-textarea placeholder=""  
      v-model="result"
      :disabled="true"
      style="height: 98%;"
      ></lay-textarea>
 
    </lay-footer>
  </lay-layout>

</template>

<script lang="ts">
import { ref, watch,reactive,onMounted } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";  
import MyWin from "../../library/MyWin"; 
import MyPage from "../../library/MyPage"; 
import {MyExcel} from "../../library/MyExcel"; 
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport"; 
import MyData from "../../library/MyData"; 
import {DateUtil} from "../../library/DateUtil"; 
export default {
  props:{
      data: Array 
  },

  setup(prop:any, context:any) {
    const selectedKeys = ref<Array<string>>([]) 
    const selectedObjs=ref<Array<any>>([]);
    const defaultToolbar = ref(false)
    // const page = ref(  new MyPage()  ); 
    const columns = [
      {
        title: '复选',
        width: '50px',
        type: 'checkbox',
        fixed: 'left'
      },
      
      {
        title: '字段名',
        width: '100px' ,
        key: 'EName'
      },
      {
        title: '名称',
        width: '100px', 
        key: 'Name' 
      },
      {
        title: '类型',
        width: '100px',
        key: 'LX' 
      } ,
      {
        title: '备注',
        width: '200px',
        key: 'Memo' 
      } 
    ]

    
    prop.data.forEach((element: { Id: any; EName: any; }) => {
      element.Id=element.EName;
    });
    const dataSource = ref(prop.data);
   
    const rowClick = function (data: any) {}

    const rowDoubleClick = function (data: any) {}

    const result=ref<string>("");
    const createCol=()=>{
        if(IsJava.value){
          createColJava();
          return;
        }
        let s='';
        selectedObjs.value.forEach(element =>{ 
           
          s+=`

[Comment("${element.Name}")]`;
          if(element.IsRequired){
            s+=`
[Required(ErrorMessage = "{0}不能为空")]`;
          }
          
          if(element.VMax!=""){
            s+=`
[MaxLength(${element.VMax})]`;
          }

          if(element.Memo!=""){
            s+=`
[Memo("${element.Memo}")]`;
          }
          s+=`
public `;
          switch(element.LX){
            case "Int32":
              s+=` int `;
              break;
            case "Int64":
              s+=` long `;
              break;
            case "DateTime":
              s+=` DateTime `;
              break;
            case "Decimal":
              s+=` Decimal `;
              break;
            default:
              s+=` string `;
              break;
          }
          s+=` ${element.EName} `;
          s+=` {get;set;} `;
        })
        result.value=s; 
    }
    const createColJava=()=>{
        let s='';
        selectedObjs.value.forEach(element =>{ 
          if(element.EName=="Id"){
            return 
          }
          if(element.EName=="Deleted"){
//             s+=`
// @TableLogic`;
          }
          s+=`
@Schema(name = "${element.Name}",description = "${element.Memo}")`;
          if(element.IsRequired){
            s+=`
@NotBlank(message = "${element.Name}不能为空")            `;
          }
          if(element.IsNum){

            if(element.VMax!=""){
            s+=`
@Max(value = ${element.VMax},message ="最大值${element.VMax}")`;
            }

            if(element.VMin!=""){
            s+=`
@Min(value = ${element.VMin},message ="最小值${element.VMin}")`;
            }


          }else{
            if(element.VMax!=""){
            if(element.VMin==""){
              element.VMin="0";
            }
            s+=`
@Size(min =${element.VMin},max = ${element.VMax},message = "长度范围${element.VMin}-${element.VMax}")`;
          }

          }
           
          s+=`
private `;
          switch(element.LX){
            case "Int32":
            case "java.lang.Integer":
              s+=` Integer `;
              break;
            case "Int64":
            case "java.lang.Long":
              s+=` Long `;
              break;
            case "DateTime":
            case "java.util.Date":
              s+=` Date `;
              break;
            case "Decimal":
              s+=` BigDecimal `;
              break;
            case "Boolean":
            case "boolean":
              s+=` boolean`;
              break;
            default:
              s+=` String `;
              break;
          }
          s+=` ${element.EName} `;
          if(element.IsStr){
            s+=`="";`;
          }else if(element.IsNum){
            if(element.LX=="Int64"
            || element.LX== "java.lang.Long"){
              s+=`=0L;`;  
            }else{
              s+=`=0;`;
            }            
          }else{
            s+=`;`;
          }
          s+=`
`;
        })
        result.value=s; 
    }
    watch(
      selectedKeys,(newVal:Array<string>,oldVal:Array<string>)=>{
         
        let addVal=MyData.keyAdd(oldVal,newVal)  ;
        let removeVal=MyData.keyRemove(oldVal,newVal)  ;
         
        removeVal.forEach(element=>{
            MyData.removeDataById(selectedObjs.value,element);          
        })

        addVal.forEach(element=>{
            let m=MyData.getById(selectedObjs.value,element);
            if(!m){
              m=MyData.getById(dataSource.value,element);
              selectedObjs.value.push(m)
            }             
        })
        createCol();
      }
    )
    onMounted(() =>{  
     
    });
    const IsJava=ref<boolean>(false);
    return {
      columns,
      dataSource,
      selectedKeys, 
      defaultToolbar,
      //page,
      rowClick,
      rowDoubleClick,
      
      selectedObjs,
      createCol,
      result,

      IsJava,
    }
  }
}
</script>
<style scoped>
.Pop .layui-footer,
.Pop .layui-header {
  height: 200px;
  text-align: center; 
   
}  
</style>